﻿using APPFrameworkStudy.Domain;
using APPFrameworkStudy.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace APPFrameworkStudy.App
{
    class Program
    {
        static OrderServices os = new OrderServices();

        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("\r\n\r\n");
                Console.WriteLine("1:创建订单");
                Console.WriteLine("2:获取今日订单");
                Console.WriteLine("3:删除订单");
                Console.WriteLine("4:更新订单信息");
                Console.WriteLine("5:分页获取订单并排序");
                Console.WriteLine("6:分页获取订单");
                Console.WriteLine("7:");
                string cmd = Console.ReadLine();
                Process(cmd);
            }
        }

        static Order order = null;
        static void Process(string cmd)
        {
            int total = 0, pageIndex = 1, pageSize = 10;

            switch (cmd)
            {
                case "1":
                    Console.WriteLine("创建订单");
                    order = new Order();
                    order.OrderItems.Add(new Product() { Name = "面包", Price = 3.5m });
                    order.OrderItems.Add(new Product() { Name = "纯牛奶", Price = 3.5m });
                    Console.WriteLine(os.CreateOrder(order));
                    break;
                case "2":
                    Console.WriteLine("今日订单：");
                    foreach (var item in os.GetTodayOrders())
                    {
                        Console.WriteLine(item);
                        foreach (var item2 in item.OrderItems)
                        {
                            Console.WriteLine("商品：" + item2.Name);
                        }
                    }
                    break;
                case "3":
                    Console.WriteLine("删除订单");
                    //这里有个细节需要注意，如果待删除对象的外键属性（OrderItems）包含了外键数据，将导致删除失败，
                    //可以清空外键数据或者实例化一个空的对象，并复制ID即可删除。这里实例化一个空对象
                    Order tmp = new Order() { ID = order.ID };
                    Console.WriteLine(os.DeleteOrder(tmp));
                    break;
                case "4":
                    Console.WriteLine("更新订单");
                    order.ShipAmount = 5m;
                    Console.WriteLine(os.UpdateOrder(order));
                    break;
                case "5":
                    Console.WriteLine("分页获取订单-排序");
                    foreach (var item in os.GetPageByOrder(pageIndex, pageSize, out total))
                    {
                        Console.WriteLine(item);
                        foreach (var item2 in item.OrderItems)
                        {
                            Console.WriteLine("商品：" + item2.Name);
                        }
                    }
                    Console.WriteLine(string.Format("当前第{0}页，每页{1}条，一共:{2}条记录。", pageIndex, pageSize, total));
                    break;
                case "6":
                    Console.WriteLine("获取订单");
                    foreach (var item in os.GetPage(pageIndex, pageSize, out total))
                    {
                        Console.WriteLine(item);
                        foreach (var item2 in item.OrderItems)
                        {
                            Console.WriteLine("商品：" + item2.Name);
                        }
                    }
                    Console.WriteLine(string.Format("当前第{0}页，每页{1}条，一共:{2}条记录。", pageIndex, pageSize, total));
                    break;
                case "7":

                    break;
                default:
                    Console.WriteLine("//////未知命令/////");
                    break;
            }
        }
    }
}
